package br.trainee.web.componentes;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import br.trainee.web.util.ResponseWrapper;

public class HeaderTag extends BodyTagSupport{
	
	private String headerClass;
	private String titleClass;
	
	@Override
	public int doEndTag() throws JspException {

		String corpo = getBodyContent().getString();
		
		JspWriter saida = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
		
		PanelTag panel = (PanelTag) getParent();
		
		request.setAttribute("id", panel.getId());
		request.setAttribute("close", panel.getClose());
		request.setAttribute("headerClass", headerClass);
		request.setAttribute("titleClass", titleClass);
		request.setAttribute("body", corpo);
		
		ResponseWrapper responseWrapper = new ResponseWrapper(response);
		
		RequestDispatcher req = request.getRequestDispatcher("/resources/componentes-jsp/header.jsp");
		
		try {
			
			//faz a mesma função do forward, so que podendo fazer alterações apos enviar a requisição
			req.include(request, responseWrapper);
			String conteudoSaida = responseWrapper.getString();
			saida.println(conteudoSaida);
			
		} catch (ServletException | IOException e1) {

			e1.printStackTrace();
		}
		
		return super.doEndTag();
	}
	

	public String getHeaderClass() {
		return headerClass;
	}

	public void setHeaderClass(String headerClass) {
		this.headerClass = headerClass;
	}

	public String getTitleClass() {
		return titleClass;
	}

	public void setTitleClass(String titleClass) {
		this.titleClass = titleClass;
	}

}
